<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> 
<head>
<title>Reflection Emit Sample</title>
<link rel="stylesheet" type="text/css" href="../../../../docs/rotor.css">
</head>

<body>		


<h1> Reflection Emit Sample</h1>


<h2>Sample Overview</h2>


<p>This sample demonstrates how to use reflection emit. Reflection emit is a 
runtime feature that allows code to create dynamic assemblies, modules and 
types. These types can be created for the purpose of creating dynamic instances 
of them and using them. Reflection emit can also be used to generate an 
assembly and persist it to disk as an executable file or DLL. </p>


<p>The sample allows you several interactive options:</p>


<ul>
  <li>Create and test a dynamic type.</li>
  <li>Create and save a type to disk.</li>
  <li>Create two dynamic assemblies and test them.</li>
  <li>Load the saved assembly from the second option and execute it.</li>
</ul>


<p>The user can run the application multiple times to explore the different 
options.&nbsp; </p>


<p>The following briefly describes the classes and technologies used by this 
sample. </p>


<ul>
  <li><b>Reflection.Emit</b><ul>
    <li><b>AppDomain</b><ul>
    <li>The sample uses the AppDomain type to create a 
    dynamic assembly in the sample's application domain. It does this by calling 
    DefineDynamicAssembly(). </li>
  </ul>
    </li>
    <li><b>AssemblyBuilder</b><ul>
    <li>Used to build a 
    dynamic assembly. It can be used to create an assembly for immediate use, or 
    a dynamic assembly can be persisted to an assembly DLL or .exe file. </li>
  </ul>
    </li>
    <li><b>AssemblyName</b><ul>
    <li>Used to define the unique identity of an assembly. The sample uses this 
    type in the simplest possible way by giving the dynamic assembly the text 
    name &quot;EmittedAssembly&quot;. </li>
  </ul>
    </li>
    <li><b>ModuleBuilder</b><ul>
    <li>Used to build a dynamic module in the dynamic assembly. </li>
  </ul>
    </li>
    <li><b>TypeBuilder</b><ul>
    <li>This Type-derived type is used to build a type dynamically. From it you 
    can request instances of the <b>FieldBuilder</b>, <b>ConstructorBuilder</b>, 
    and <b>MethodBuilder</b> types. Using these types, the sample generates a 
    complete type. </li>
  </ul>
    </li>
    <li><b>FieldBuilder</b><ul>
    <li>Used to create a field in a dynamic type. </li>
  </ul>
    </li>
    <li><b>ConstructorBuilder</b><ul>
    <li>Used to define a constructor in a dynamic type. </li>
  </ul>
    </li>
    <li><b>MethodBuilder</b><ul>
    <li>Used to define a method in a dynamic type. </li>
  </ul>
    </li>
    <li><b>ILGenerator</b><ul>
    <li>Both MethodBuilder and ConstructorBuilder implement a method called 
    GetILGenerator(). These methods return an instance of the ILGenerator type 
    which is used to dynamically generate IL-code for a type.</li>
  </ul>
    </li>
    <li><b>Thread</b><ul>
    <li>The static member function GetDomain() is used to get an instance of the 
    AppDomain type for the current thread. </li>
  </ul>
    </li>
  </ul>
  </li>
</ul>


<h2>Sample Source and Build Output Locations</h2>


<p>The sample source is found in sscli20\samples\howto\reflection\emit.&nbsp; </p>


<p>The source file is:</p>


<ul class="none">
  <li><a href="reflectionemit.cs">reflectionemit.cs</a></li>
</ul>


<p>The build output location is %_NTTREE%\samples\howto\reflection\emit.&nbsp; 
The output file is an executable assembly named reflectionemit.exe.</p>


<h2>Building the Sample</h2>


<p>All samples are built from the buildall script.&nbsp;  </p>


<p>You can also build all the 
samples by switching to the root of the sample directory, sscli20\samples, and typing 
<code>build -c</code>.</p>


<p>You can build this specific sample  by switching to the sample directory and typing 
<code>build -c</code>.</p>


<h2>Running the Sample</h2>


<p>These steps require that the Shared Source CLI (SSCLI) be already built and 
functional.</p>


<ol>
  <li>Run  env.bat. </li>
  <li>Switch to the %_NTTREE%\samples\howto\reflection\emit directory.</li>
  <li>Type the following command:<blockquote>


<p>clix reflectionemit.exe</p>


  </blockquote>


  </li>
</ol>


<hr>


<p><i>Copyright (c) 2006 Microsoft Corporation. All rights reserved.</i></p>
</body> 
</html>